package pl.edu.agh.uddiProxy.example;

import java.util.HashSet;
import java.util.Set;

import pl.edu.agh.uddiProxy.UDDIClient;
import pl.edu.agh.uddiProxy.dao.DAOHelper;
import pl.edu.agh.uddiProxy.types.TModel;
import dk.itst.uddi.client.query.FindBindingResult;
import dk.itst.uddi.client.query.FindBusinessResult;
import dk.itst.uddi.client.query.FindServiceQuery;
import dk.itst.uddi.client.query.FindServiceResult;
import dk.itst.uddi.client.types.core.BindingTemplate;
import dk.itst.uddi.client.types.core.BusinessInfo;
import dk.itst.uddi.client.types.core.ServiceInfo;
import dk.itst.uddi.client.types.core.TModelInstanceInfo;

public class Sample {
	private static UDDIClient client = DAOHelper.getUDDIClient();
	
	private String getWSDL(String tModelKey) {
		return client.getInquiryClient().getWSDL(tModelKey) ;
	}
	
	private BusinessInfo[] getBusiness(String businessName) {
		FindBusinessResult findBusinessResult = client.getInquiryClient().findBusiness().addName(businessName).addQualifier(FindServiceQuery.APPROXIMATE_MATCH).execute();
		return findBusinessResult.getBusinessInfos();
	}
	
	private ServiceInfo[] getService(String serviceName) {
		FindServiceResult findServiceResult = client.getInquiryClient().findService().addName(serviceName).addQualifier(FindServiceQuery.APPROXIMATE_MATCH).execute();
		return findServiceResult.getServiceInfos();
	}
	
	private BindingTemplate[] getBinding(ServiceInfo serviceInfo) {
		FindBindingResult findBindingResult = client.getInquiryClient().findBinding().setServiceKey(serviceInfo.getServiceKey()).execute();
		return findBindingResult.getTemplates();
	}
	
	private TModelInstanceInfo[] getTModelInstanceDetails(BindingTemplate bindingTemplate) {
		if (bindingTemplate.getTModelInstanceDetails() != null) {
			return bindingTemplate.getTModelInstanceDetails().getTModelInstanceInfoArray();
		}
		return null;
	}
	
	public static void main(String[] args) {
		Sample sample = new Sample();
		
		Set<String> set = new HashSet<String>(); 
		
		
		//znajdz pliki WSDL dla kazdego serwisu ktorego nazwa zaczyna sie od UDDI
		
		for (ServiceInfo serviceInfo : sample.getService("UDDI %")) {
			for (BindingTemplate bindingTemplate : sample.getBinding(serviceInfo)) {
				for (TModelInstanceInfo modelInstanceInfo : sample.getTModelInstanceDetails(bindingTemplate)) {
					set.add(modelInstanceInfo.getTModelKey());
					System.out.println("tModelKey: " + modelInstanceInfo.getTModelKey() + ",\tWSDL: " + sample.getWSDL(modelInstanceInfo.getTModelKey()));
				}
			}
		}
		
		String[] array = new String[set.size()];
		
		System.out.println("--------------------------------------------");
		
		//znajdz pliki WSDL dla kazdego serwisu ktorego parametr dynamiczy serviceRank jest wiekszy od 5, wyniki beda posortowane
		
		for (TModel model : client.getInquiryClient().getTModels(set.toArray(array), "serviceRank > 5")) {
			System.out.println("tModelKey: " + model.getModel().getTModelKey() + ",\tWSDL: " + sample.getWSDL(model.getModel().getTModelKey()) + 
					",\tserviceRank: " + model.getDynamicParameter().getParameterValue("serviceRank").getValue());
		}
		
		
	}
}
